package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.itheima.dto.DishDto;
import com.itheima.entity.Dish;
import com.itheima.entity.DishFlavor;
import com.itheima.mapper.DishMapper;
import com.itheima.service.IDishFlavorService;
import com.itheima.service.IDishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Dish业务层实现类
 *
 * @author hua
 * @since 2022/11/3 22:21
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements IDishService {
    @Autowired
    private IDishFlavorService flavorService;
   @Override
   @Transactional
    public void saveWithFlavor(DishDto dishDto) {
       //当新增\修改\删除 并且 当操作两张表或者两张以上以上的时候,要记得开启事务
        //1.保存菜品基本信息
        //单一原则
        Dish dish = new Dish();
        //第一个参数是源数据
        //第二个参数是目标
        //BeanUtils.copyProperties可做到类复制
        BeanUtils.copyProperties(dishDto, dish);
        this.save(dish);
        //当保存完了菜品表之后,如果后面的内容出现了异常情况、
       //那么这个保存菜品表的保存操作,也会回滚，回滚指的是,数据库会回滚到保存纸条数据之前
        //2.保存菜品口味
       //需要这个菜品的id
        Long dishId = dish.getId();
       //保存菜品口味之前,要给菜品口味的dishId赋值
       /*如果要是不赋值的话,那么这些口味实际上就不属于任何菜品了,那和我们保存这次菜品信息的操作
       也就没有关联了*/
       //size大于0,就代表前端传入了口味信息
        if (dishDto.getFlavors().size() > 0) {
            for (DishFlavor flavor : dishDto.getFlavors()) {
                //菜品id
                flavor.setDishId(dishId);
            }
            //调用保存菜品口味信息
            this.flavorService.saveBatch(dishDto.getFlavors());
        }
    }

    /**
     * 查询相应的菜品信息
     * @param id
     * @return
     */
    @Override
    public DishDto getBYIdWithFlavor(long id) {
      //查询菜品的基本信息
        Dish dish = this.getById(id);
        //校验判断
        if (dish ==null) {
            throw new ClassCastException("传入的参数不正确");
        }
        //构建dishDto,并赋值
        DishDto dishDto = new DishDto();
        //dish -->dishDto
        BeanUtils.copyProperties(dish,dishDto);
        //查询当前菜品对应的口味信息,赋值给dishDto对象
        //要去菜品口味表查询
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        //select * from dish_Flavor where dish_id=dishId
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        //当前菜品口味信息
        List<DishFlavor> dishFlavors = this.flavorService.list(queryWrapper);
        //赋值给dishDto对象
        dishDto.setFlavors(dishFlavors);
        return dishDto;
    }

    /**
     *  更新数据
     * @param dto
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dto) {
    //1.先更新Dish表
        Dish dish= new Dish();
//在dto中取到我们更新的值
        BeanUtils.copyProperties(dto,dish);
        //更新dish表
        updateById(dish);
        //2.删除菜品口味
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        //根据菜品id删除掉所有跟他有关的菜品口味

        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        // delete from dish_flavor where dish_id=dishId
        flavorService.remove(queryWrapper);
        //添加新的口味
        //3.把dish重新赋值给flavors口味
        dto.getFlavors().forEach(item -> item.setDishId(dish.getId()));
        //for等价于上面的
//        for (DishFlavor flavor : dto.getFlavors()) {
//            flavor.setDishId(dish.getId());
//        }
        //4.添加新的口味
        this.flavorService.saveBatch(dto.getFlavors());
    }
}